package org.wheel.plugins.toolkit.sql.function.plugin;

import cn.hutool.core.util.StrUtil;
import org.wheel.plugins.toolkit.sql.enums.Dialect;
import org.wheel.plugins.toolkit.sql.enums.SqlFunction;
import org.wheel.plugins.toolkit.sql.plugin.FunctionPlugin;

import java.util.List;

/**
 * JSON_EXTRACT 函数插件
 */
public class JsonExtractPlugin implements FunctionPlugin {

    @Override
    public String name() {
        return SqlFunction.JSON_EXTRACT.name();
    }

    @Override
    public boolean isWindow() {
        return false;
    }

    @Override
    public String toSql(List<String> argSql, Dialect dialect) {
        String col = argSql.get(0);
        String path = argSql.get(1);
        return switch (dialect) {
            case MYSQL -> StrUtil.format("JSON_EXTRACT({}, {})", col, path);
            case POSTGRESQL -> StrUtil.format("{} -> {}", col, path);
            default -> throw new UnsupportedOperationException(dialect.name());
        };
    }
}